package com.kgc.web;

import com.kgc.dao.paymentDao;
import com.kgc.dao.serviceDao;
import com.kgc.entity.Gonggao;
import com.kgc.entity.Payment;
import com.kgc.entity.Repair;
import com.kgc.entity.Tousu;
import com.kgc.utils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "serviceServlet", value = "/serviceServlet")
public class serviceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        String opt = request.getParameter("opt");
        PageUtils pageUtils = new PageUtils();
        System.out.println("opt = " + opt);
        if ("cy3".equals(opt)) {
            String cy3name = request.getParameter("cy3name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int count = new serviceDao().countGG();

            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(count);
            // 查询用户数据
            List<Gonggao> list = new serviceDao().selectGG(pageUtils, cy3name);


            writer.write(R.success("", count, list).toJSON());
        } else if ("cy4".equals(opt)) {
            String cy4name = request.getParameter("cy4name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int count = new serviceDao().countWX();

            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(count);
            // 查询用户数据
            List<Repair> repairs = new serviceDao().selectWX(pageUtils, cy4name);


            writer.write(R.success("", count, repairs).toJSON());
        }else if ("cy5".equals(opt)) {
            String cy5name = request.getParameter("cy5name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            int count = new serviceDao().countTS();

            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(count);
            // 查询用户数据
            List<Tousu> tousus = new serviceDao().selectTS(pageUtils, cy5name);


            writer.write(R.success("", count, tousus).toJSON());
        }else if ("cy3edit".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String status = request.getParameter("status");
            int count = new serviceDao().updateGGByID(id, title, content, status);
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }else if ("cy3delete".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            int count = new serviceDao().deleteGGByID(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        }else if ("cy4edit".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            String result = request.getParameter("result");
            String content = request.getParameter("content");
            String status = request.getParameter("status");
            int count = new serviceDao().updateWXByID(id, result, content, status);
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }else if ("cy4delete".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            int count = new serviceDao().deleteWXByID(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        }else if ("cy5edit".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            String result = request.getParameter("result");
            String content = request.getParameter("content");
            String status = request.getParameter("status");
            int count = new serviceDao().updateTSByID(id, result, content, status);
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }else if ("cy5delete".equals(opt)) {
            Integer id =Integer.parseInt(request.getParameter("id"));
            int count = new serviceDao().deleteTSByID(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
